﻿//-----------------------------------------------------------------------
// <copyright file="Form1.cs" company="JiYou">
//     Copyright (c)
// </copyright>
// <author>Jill Ye</author>
//-----------------------------------------------------------------------
namespace PNunitAgentConfigurationEditor
{
    using System;
    using System.Windows.Forms;
    using PNunitToolsClass;

    /// <summary>
    /// The main form of pnunit agent configuration editor
    /// </summary>
    public partial class Form1 : Form
    {
        /// <summary>
        /// the temp agentConfig value;
        /// </summary>
        private AgentConfig agentConfig;

        /// <summary>
        /// Initializes a new instance of the <see cref="Form1"/> class.
        /// </summary>
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Handles the Click event of the Button_agent_load control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Button_agent_load_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "conf files (*.conf)|*.conf";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    this.agentConfig = AgentConfig.Load(openFileDialog.FileName);
                    MessageBox.Show("Load succeed!");
                    numericUpDown_agent_port.Value = this.agentConfig.Port;
                    textBox_agent_pathToAssemblies.Text = this.agentConfig.PathToAssemblies;
                    numericUpDown_agent_port.Enabled =
                        textBox_agent_pathToAssemblies.Enabled =
                        button_agent_select.Enabled =
                        button_agent_save.Enabled  = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(string.Format("Load failed! {0}", ex.Message));
                }
            }
        }

        /// <summary>
        /// Handles the Click event of the Button_agent_new control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Button_agent_new_Click(object sender, EventArgs e)
        {
            this.agentConfig = new AgentConfig(10000);
            numericUpDown_agent_port.Value = this.agentConfig.Port;
            textBox_agent_pathToAssemblies.Text = this.agentConfig.PathToAssemblies;
            numericUpDown_agent_port.Enabled =
                textBox_agent_pathToAssemblies.Enabled =
                button_agent_select.Enabled =              
                button_agent_save.Enabled = true;
        }

        /// <summary>
        /// Handles the Click event of the Button_agent_save control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Button_agent_save_Click(object sender, EventArgs e)
        {
            this.agentConfig.Port = Convert.ToInt32(numericUpDown_agent_port.Value);
            this.agentConfig.PathToAssemblies = textBox_agent_pathToAssemblies.Text;
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "conf files (*.conf)|*.conf";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    this.agentConfig.Save(saveFileDialog.FileName);
                    MessageBox.Show("Save succeed!");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(string.Format("Save failed! {0}", ex.Message));
                }
            }
        }

        /// <summary>
        /// Handles the Click event of the Button_agent_select control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Button_agent_select_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderbrowerdialog = new FolderBrowserDialog();
            if (folderbrowerdialog.ShowDialog() == DialogResult.OK)
            {
                textBox_agent_pathToAssemblies.Text = folderbrowerdialog.SelectedPath;
                if (textBox_agent_pathToAssemblies.Text == string.Empty)
                {
                    textBox_agent_pathToAssemblies.Text = ".";
                }
            }
        }
    }
}
